package com.yksj.healthtalk.ui.interestwall;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;

import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.entity.TagEntity;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.interestwall.StairCategoryChooseFragment.CategoryChooseAdapter;
import com.yksj.healthtalk.utils.ToastUtil;
import com.yksj.healthtalk.utils.XmlCategoryParserUtils;
/**
 * 兴趣墙搜索
 * @author root
 *
 */
public class InsterswallTypeChooseActivity extends BaseFragmentActivity implements
	OnItemClickListener,OnClickListener {
	
	Fragment mFirstFragment;
	Fragment mSecendFragment;
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.installwall_seach_activity);
		initTitle();
		initView();
	}
	
	private void initView(){
		titleLeftBtn.setOnClickListener(this);
		titleTextV.setText("选择分类");
		titleRightBtn2.setOnClickListener(this);
		titleRightBtn2.setText("确定");
		titleRightBtn2.setVisibility(View.VISIBLE);
		onChangeFragment(0);
	}
	String isFirst="";
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		
		TagEntity entity = (TagEntity) parent.getItemAtPosition(position);
		if(getSupportFragmentManager().getBackStackEntryCount() == 0){
			if(position == 0){//第一个全部
				Intent intent = new Intent();
				intent.putExtra("entity",entity);
				setResult(RESULT_OK,intent);
				finish();
					return;
				}
			isFirst=entity.getId();
			createSecendFragment(entity.getId());
			onChangeFragment(1);
		}else{
			if(isFirst.equals("")){
				Intent intent = new Intent();
				intent.putExtra("entity",entity);
				setResult(RESULT_OK,intent);
				finish();	
			}else{
				entity.setUpperId(isFirst);
				Intent intent = new Intent();
				intent.putExtra("entity",entity);
				setResult(RESULT_OK,intent);
				finish();	
			}
		}
	}
	
	private void createSecendFragment(String id){
		mSecendFragment = SecendTypeChooseFragment.instance(id,this);
	}
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.title_back://
			int count = getSupportFragmentManager().getBackStackEntryCount();
			if(count == 0){//在第一节菜单
				setResult(RESULT_CANCELED,null);
				finish();
			}else{//在二级
				getSupportFragmentManager().popBackStack();
				onChangeFragment(0);
			}
			break;
		case R.id.title_right2://确定
			String str = ((FirstTypeChooseFragment)mFirstFragment).mEditText.getText().toString();
			if(TextUtils.isEmpty(str)){
				ToastUtil.showBasicShortToast(getApplicationContext(), "请输入图片全程或部分名称");
			}else{
			Intent intent=new Intent();
			TagEntity entity = new TagEntity("Name", str);
			intent.putExtra("entity", entity);
			setResult(str.length() == 0 ? RESULT_CANCELED:RESULT_OK,intent);
			finish();}
			break;
		}
	}
	
	private void onChangeFragment(int index){
		FragmentManager fg = getSupportFragmentManager();
		FragmentTransaction ft = fg.beginTransaction();
		if(index == 0){//返回到第一节
			findViewById(R.id.title_right).setVisibility(View.VISIBLE);
			if(mFirstFragment == null)mFirstFragment = FirstTypeChooseFragment.instance(this);
			ft.replace(R.id.fragmentcontent,mFirstFragment);
		}else if(index == 1){//第二级
			findViewById(R.id.title_right).setVisibility(View.GONE);
			ft.replace(R.id.fragmentcontent,mSecendFragment);
			ft.addToBackStack(null);
		}
		ft.commitAllowingStateLoss();
	}
	
	
	@SuppressLint("ValidFragment")
	static class FirstTypeChooseFragment extends Fragment implements OnItemClickListener{
		OnItemClickListener mClickListener;
		private ListView stairListView;
		EditText mEditText;
		private CategoryChooseAdapter stairAdapter;
		public FirstTypeChooseFragment(OnItemClickListener listener) {
			this.mClickListener = listener;
		}
		
		public static FirstTypeChooseFragment instance(OnItemClickListener listener) {
			FirstTypeChooseFragment fragment = new  FirstTypeChooseFragment(listener);
			return fragment;
		}
		
		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View view = inflater.inflate(R.layout.interest_first_level_layout, container,false);
			initData();
			initView(view);
			return view;
		}
		
		public void initView(View view) {
			mEditText = (EditText)view.findViewById(R.id.seach_text);
			mEditText.setVisibility(View.VISIBLE);
			stairListView = (ListView) view.findViewById(R.id.category_listview);
			stairListView.setAdapter(stairAdapter);
			stairListView.setOnItemClickListener(this);
//			stairListView.setSelector(R.color.transparent);
			
		
		}
		
		public void initData() {
			//一级分类内的第一个item，点击之后不进入二级分类直接返回主界面刷新
			TagEntity entity = new TagEntity("0", "全部");
			entity.setUpperId("0");
			List<TagEntity> stairData = new ArrayList<TagEntity>();
			stairData.add(entity);
			stairData.addAll(XmlCategoryParserUtils.getCategoryData(getResources(), R.xml.interestwall_class_store, 0));
			stairAdapter = new CategoryChooseAdapter(this.getActivity(), stairData);
		}
		
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			if(mClickListener != null)mClickListener.onItemClick(parent, view, position, id);
		}
		

	}
	
	@SuppressLint("ValidFragment")
	static class SecendTypeChooseFragment extends Fragment implements OnItemClickListener{
		OnItemClickListener mClickListener;
		private ListView stairListView;
		
		@SuppressLint("ValidFragment")
		public SecendTypeChooseFragment(OnItemClickListener listener) {
			this.mClickListener = listener;
		}
		public static SecendTypeChooseFragment instance(String id,OnItemClickListener listener) {
			SecendTypeChooseFragment fragment  = new SecendTypeChooseFragment(listener);
			Bundle bundle = new Bundle();
			bundle.putString("id",id);
			fragment.setArguments(bundle);
			return fragment;
		}
		
		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View view = inflater.inflate(R.layout.interest_first_level_layout,null);
			stairListView = (ListView) view.findViewById(R.id.category_listview);
			return view;
		}
		
		
		@Override
		public void onActivityCreated(Bundle savedInstanceState) {
			super.onActivityCreated(savedInstanceState);
			String id = getArguments().getString("id");
			initData(id);
			stairListView.setOnItemClickListener(this);
//			stairListView.setSelector(R.color.transparent);
		}

		public void initData(String id) {
			//一级分类内的第一个item，点击之后不进入二级分类直接返回主界面刷新
			TagEntity entity = new TagEntity("0", "全部");
			entity.setUpperId("0");
			List<TagEntity> stairData = new ArrayList<TagEntity>();
			stairData.add(entity);
			stairData.addAll(XmlCategoryParserUtils.getCategoryData(getResources(), R.xml.interestwall_class_store,Integer.parseInt(id)));
			CategoryChooseAdapter stairAdapter = new CategoryChooseAdapter(this.getActivity(), stairData);
			stairListView.setAdapter(stairAdapter);
		}
		
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			if(mClickListener != null)mClickListener.onItemClick(parent, view, position, id);
		}
	}
}
